/*
* $Id: to_0_3_0.cpp 174 2010-10-17 16:53:31Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include <Common/include.h>
#include <Database/Database.h>
#include <Database/Tables/TableNames.h>

void to_0_3_0(CDatabase* pDb){
	//update inspect table
	if(!pDb->IsExistTable(INSPECT_TABLE_NAME)){
		std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
		pDb->ExecRequest(strRequest + INSPECT_TABLE_NAME +
			_T("(id_journal INTEGER NOT NULL, id_housing INTEGER, name_housing TEXT, honey INTEGER NULL,")\
			_T("brood INTEGER NULL, open_brood INTEGER NULL, close_brood INTEGER NULL, goods_honey INTEGER NULL,")\
			_T("CONSTRAINT fkJournalJournal FOREIGN KEY(id_journal) REFERENCES journal(id_journal));"));
	}else{
		std::tstring strRequest(_T("ALTER TABLE "));
		pDb->ExecRequest(strRequest + INSPECT_TABLE_NAME + _T(" ADD COLUMN name_housing TEXT;"));
	}
	//update type housing table
	if(pDb->IsExistTable(TYPE_HOUSING_TABLE_NAME)){
	std::tstring strRequest(_T("SELECT MAX(id_type) from "));
	int nMax = _ttoi(pDb->ExecRequest(strRequest + TYPE_HOUSING_TABLE_NAME)[0][0].m_strValueColumn.c_str());
	for(int i = nMax; i >= 7; i--){
		std::tstring strRequest(_T("UPDATE "));
		strRequest = strRequest + TYPE_HOUSING_TABLE_NAME + _T(" SET id_type=") + CConvertFunction::ConvertFromInt(i + 1);
		pDb->ExecRequest(strRequest + _T(" WHERE id_type=") + CConvertFunction::ConvertFromInt(i));
	}
	strRequest = _T("INSERT INTO ");
	pDb->ExecRequest(strRequest + TYPE_HOUSING_TABLE_NAME + _T(" VALUES(7,'')"));
	//update housing table
	for(int i = nMax; i >= 3; i--){
		strRequest = _T("UPDATE ");
		strRequest = strRequest + HOUSINGS_TABLE_NAME + _T(" SET type_housing=") + CConvertFunction::ConvertFromInt(i + 1);
		pDb->ExecRequest(strRequest + _T(" WHERE type_housing=") + CConvertFunction::ConvertFromInt(i));
	}
}
}